package cvi.com.reader.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.orhanobut.hawk.Hawk;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.activity.ChartsDataActivity;
import cvi.com.reader.activity.WorksDetailsAcitivity;
import cvi.com.reader.activity.WorksDetailsAcitivity2;
import cvi.com.reader.bean.HotBean;
import cvi.com.reader.tool.UtilsOnclickListen;

/**
 * Created by 梅涛 on 2020/7/24.
 */

public class HomePageListAdapter extends BaseAdapter {
    private Context context;
    private List<HotBean.RowsBean> rowsBeanList;

    public HomePageListAdapter(Context context, List<HotBean.RowsBean> rowsBeanList) {
        this.context = context;
        this.rowsBeanList = rowsBeanList;
    }

    @Override
    public int getCount() {
        return rowsBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return rowsBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viHolder = null;
        final HotBean.RowsBean rowsBean = rowsBeanList.get(i);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.item_home_hot, null);
            viHolder = new ViewHolder(view);

            view.setTag(viHolder);
        } else {
            viHolder = (ViewHolder) view.getTag();
        }

        viHolder.name.setText(rowsBean.getMaterial_name());
        String strUrl = null;
        if (Hawk.contains("ip")) {
            strUrl = Hawk.get("ip");
        }
        if (strUrl != null) {
            ImageLoader.getInstance().displayImage("http://" + strUrl + rowsBean.getCover_img(), viHolder.imge);
            //  Glide.with(context).load("http://" + strUrl + rowsBean.getCover_img()).into(viHolder.imge);
            Log.d("urlstr", "http://" + strUrl + rowsBean.getCover_img() + "====" + "http://" + strUrl + rowsBean.getBg_img());
        }
        viHolder.reader.setText("作者：" + rowsBean.getAuthor());
        viHolder.viewsNum.setText(rowsBean.getRead_times() + "");
        viHolder.preaseNum.setText(rowsBean.getClick_times() + "");
        viHolder.goRank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {
                    Intent intent = new Intent(context, ChartsDataActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt("material_id", rowsBean.getId());
                    bundle.putString("material_name", rowsBean.getMaterial_name());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            }
        });
        viHolder.goDateil.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {
                    Intent intent = new Intent(context, WorksDetailsAcitivity2.class);
                    context.startActivity(intent);
                    Hawk.put("WorksDetails", rowsBean);
                }
            }
        });

        return view;
    }


    static class ViewHolder {
        @BindView(R.id.imge)
        ImageView imge;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.reader)
        TextView reader;
        @BindView(R.id.views_num)
        TextView viewsNum;
        @BindView(R.id.prease_num)
        TextView preaseNum;
        @BindView(R.id.go_dateil)
        TextView goDateil;
        @BindView(R.id.go_rank)
        TextView goRank;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
